package com.lwy.qiuchao.integration.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lwy.qiuchao.integration.entity.AllDurationEntity;
import com.lwy.qiuchao.integration.entity.IntegrationEntity;
import com.lwy.qiuchao.integration.service.AllDurationService;
import com.lwy.qiuchao.integration.service.IntegrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwy.common.utils.PageUtils;
import com.lwy.common.utils.Query;

import com.lwy.qiuchao.integration.dao.DurationDao;
import com.lwy.qiuchao.integration.entity.DurationEntity;
import com.lwy.qiuchao.integration.service.DurationService;


@Service("durationService")
public class DurationServiceImpl extends ServiceImpl<DurationDao, DurationEntity> implements DurationService {

    @Autowired
    IntegrationService integrationService;

    @Autowired
    AllDurationService allDurationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DurationEntity> page = this.page(
                new Query<DurationEntity>().getPage(params),
                new QueryWrapper<DurationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void infield(Map<String, Object> params) {
        String memberIdStr = (String) params.get("memberId");
        Long memberId = new Long(memberIdStr);
        String fieldIdStr = (String) params.get("fieldId");
        Long fieldId = new Long(fieldIdStr);

//        进球场
        if(params.get("createtime")!=null){
            String createtimeStr = (String) params.get("createtime");
            Long createtimeLong = new Long(createtimeStr);
            Date createtime = new Date(createtimeLong);
            DurationEntity entity = new DurationEntity();
            entity.setMemberId(memberId);
            entity.setFieldId(fieldId);
            entity.setCreateTime(createtime);
            //todo constant version
            entity.setVersion(0);
            this.save(entity);
        }

//        离开球场
        if(params.get("finishtime")!=null){
            String finishtimeStr = (String) params.get("finishtime");
            Long finishtimeLong = new Long(finishtimeStr);
            Date finishtime = new Date(finishtimeLong);
            QueryWrapper<DurationEntity> queryWrapper = new QueryWrapper<DurationEntity>().eq("member_id", memberId)
                    .and(item -> {
                        item.eq("field_id", fieldId)
                                .and(obj -> {
                                    obj.eq("version", 0);
                                });
                    });
            DurationEntity entity = getOne(queryWrapper);
            Long createtime = entity.getCreateTime().getTime();
            //计算时长
            Long inte = finishtimeLong - createtime;
            Integer inteValue = inte.intValue() / (1000 * 60 * 10);
            //更新duration表中version为0的数据
            entity.setFinishTime(finishtime);
            entity.setInteValue(inteValue);
            //todo constant
            entity.setVersion(1);
            update(entity,queryWrapper);

            //操作integration表
            IntegrationEntity integrationEntity = integrationService.getOne(new QueryWrapper<IntegrationEntity>().eq("member_id", memberId));
            if(integrationEntity == null){
                IntegrationEntity entity1 = new IntegrationEntity();
                entity1.setMemberId(memberId);
                entity1.setTotalValue(inteValue);
                integrationService.getBaseMapper().insert(entity1);
            }else {
                integrationService.updateValueAdd(memberId,inteValue);
            }

            //操作时长表
            AllDurationEntity allDurationEntity = allDurationService.getOne(new QueryWrapper<AllDurationEntity>().eq("member_id", memberId).and(item -> {
                item.eq("field_id", fieldId);
            }));
            if(allDurationEntity == null){
                AllDurationEntity entity1 = new AllDurationEntity();
                entity1.setMemberId(memberId);
                entity1.setFieldId(fieldId);
                entity1.setAllTime(inte);
                allDurationService.getBaseMapper().insert(entity1);
            }else{
                allDurationService.updateTime(memberId,fieldId,inte);
            }
        }


    }
}